<template>
	<view class="resetuser">
		<tn-form 
			:model="model"
			ref="form"
			:errorType="errorType"
			:labelPosition="labelPosition"
			:labelWidth="labelWidth"
			:labelAlign="labelAlign">
			<tn-form-item 
				label="邮箱"
				prop="mail"
				leftIcon="email"
				:required="true"
				:labelPosition="labelPosition"
				:labelAlign="labelAlign">
					<tn-input
						v-model="model.mail"
						type="text" placeholder="请输入邮箱"
						:border="border"></tn-input>
				</tn-form-item>
				<tn-form-item
					label="手机号"
					prop="phone"
					leftIcon="phone-fill"
					:labelPosition="labelPosition"
					:labelAlign="labelAlign">
					<tn-input
						v-model="model.phone"
						type="number"
						placeholder="请输入手机号码"
						:border="border"></tn-input>
				</tn-form-item>
				<tn-form-item
					label="头像"
					leftIcon="my-circle-fill"
					prop="avatar"
					:labelPosition="labelPosition"
					:labelAlign="labelAlign">
					<tn-image-upload
						:fileList="fileList"
						:action="action"
						:maxCount="1"
						@on-success="imageSuccess"
						@on-list-change="imageUploadChange"></tn-image-upload>
				</tn-form-item>
			</tn-form>
			<tn-button class="tn-main-gradient-indigo" width="100%" @click="submit">提交</tn-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				errorType: ['message','border-bottom','toast'],
				labelPosition: 'left',
				labelAlign: 'right',
				labelWidth: 140,
				border: false,
				action:'http://www.lihyweb.cn/api/upload',
				fileList:[],
				model:{
					mail:'',
					phone:'',
					avatar:''
				}
			};
		},
		onShow() {
			const value = uni.getStorageSync('userInfo')
			if(value){
				this.model = {
					mail:value.mail,
					phone:value.phone,
					avatar:value.avatar
				}
				this.fileList = [value.avatar]
			}
		},
		methods:{
			//上传图片
			imageUploadChange(lists) {
				console.log(lists)
			  // this.model.photo = lists[0]
			},
			//
			imageSuccess(data, index, lists, name){
				this.model.avatar = 'http://www.lihynotes.cn:81/upload/' + data.data.newFilename
			},
			submit(){
				let userName = uni.getStorageSync('userInfo').userName
				this.$api.ResetInfo({
					userName:userName,
					...this.model
				}).then(res => {
					if(res.code == 200){
						uni.showToast({
							icon: 'none',
							mask:true,
							position: 'top',
							title: '修改成功，请重新登录'
						});
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/login/index'
							},1500)
						})
					} else {
						uni.showToast({
							icon: 'none',
							mask:true,
							position: 'top',
							title: res.message
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.resetuser{
	padding: 40rpx;
}
</style>
